# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=zlib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.8
pkgrel=9
pkgdesc="Compression library implementing the deflate compression method found in gzip and PKZIP (mingw-w64)"
arch=('any')
license=(ZLIB)
url="http://www.zlib.net/"
depends=("${MINGW_PACKAGE_PREFIX}-bzip2")
makedepends=('git')
options=('staticlibs')
source=("http://zlib.net/current/${_realname}-${pkgver}.tar.gz"
        "git+https://github.com/nmoinvaz/minizip.git"
        01-zlib-1.2.7-1-buildsys.mingw.patch
        02-no-undefined.mingw.patch
        03-dont-put-sodir-into-L.mingw.patch
        04-wrong-w8-check.mingw.patch
        05-fix-a-typo.mingw.patch
        010-unzip-add-function-unzOpenBuffer.patch
        011-Add-no-undefined-to-link-to-enable-build-shared-vers.patch
        012-Add-bzip2-library-to-pkg-config-file.patch
        013-fix-largefile-support.patch)
sha256sums=('36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d'
            'SKIP'
            'd173a6d5e0ca550bb5ee6f53a4309959413e225e73d1507a984cae52d53c4ed2'
            '44e23000cc4cb7502f303aa252be17ffee66384fe76a66607dc1f4df0a815030'
            '3d039f42194aade91dadf4174f55fd6db349fd8044db93875bed1042dcfe1f31'
            '053fd492a821aca1fcb988f8ccb8857bb62e1826a5c3e5e1794f7f5f17797e3f'
            'd0fb89b59d629a8f8a4685425542a92255633ea3126edc12d21522dc41241fb7'
            'ebbc1b8c988a6af3c0d77f404befe40abfe7c7303883b24c94d1468e685306a3'
            'ebf118d3754c1cb9a07f391bfdb24d2941bc4e11a7b3956b17fa095f97e6eae8'
            '08269402dc15bb6d1600e011633ca3ace43e2d74613fdabdd516a05f5939cc78'
            'ee72223ebde7deedaf413992b93b25bffea694d0727c936192cc40bef173800b')

prepare() {
  # Replace minizip with external
  rm -rf ${srcdir}/${_realname}-${pkgver}/contrib/minizip
  cp -r ${srcdir}/minizip/ ${srcdir}/${_realname}-${pkgver}/contrib

  cd ${srcdir}/${_realname}-${pkgver}
  grep -A 24 '^  Copyright' zlib.h > LICENSE

  patch -p2 -i ${srcdir}/01-zlib-1.2.7-1-buildsys.mingw.patch
  #patch -p2 -i ${srcdir}/02-no-undefined.mingw.patch
  patch -p2 -i ${srcdir}/03-dont-put-sodir-into-L.mingw.patch
  #patch -p2 -i ${srcdir}/04-wrong-w8-check.mingw.patch
  #patch -p2 -i ${srcdir}/05-fix-a-typo.mingw.patch

  patch -p1 -i ${srcdir}/013-fix-largefile-support.patch

  cd ${srcdir}/${_realname}-${pkgver}/contrib/minizip
  git am ${srcdir}/010-unzip-add-function-unzOpenBuffer.patch
  git am ${srcdir}/011-Add-no-undefined-to-link-to-enable-build-shared-vers.patch
  git am ${srcdir}/012-Add-bzip2-library-to-pkg-config-file.patch

}

build() {
  cd ${srcdir}/${_realname}-${pkgver}
  CHOST=${MINGW_CHOST} ./configure --prefix=${MINGW_PREFIX} --shared
  make -j1 all

  # build minizip
  pushd contrib/minizip > /dev/null
  autoreconf -fi
  CFLAGS+=" -DHAVE_BZIP2"
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-demos \
    LIBS="-lbz2"
  make
  popd > /dev/null
}

check() {
  cd ${srcdir}/${_realname}-${pkgver}
  make test
}

package() {
  cd ${srcdir}/${_realname}-${pkgver}
  make install DESTDIR="${pkgdir}"
  pushd contrib/minizip > /dev/null
  make install DESTDIR="${pkgdir}"
  popd > /dev/null
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
